package cc.wsyw126.java.stream;

import java.util.Optional;
import java.util.Stack;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Created by junpeng.yang on 16/8/24.
 */
public class StructureOfNumericalFlow {
    public static void main(String[] args) {
        IntStream.of(new int[]{1, 2, 3}).forEach(System.out::println);

        System.out.println("---------------");
        int[] integers = {1,2,3,4,5};
        IntStream.of(integers).filter(n->n>2).peek(n -> System.out.println("n2 = " + n)).filter(n -> n > 3).forEach(System.out::println);

        System.out.println("---------------");

        Stream.of(new String[]{"string", "is", "too", "short"}).sorted((o1,o2)->o1.length()>o2.length()?1:-1).forEach(System.out::println);
        System.out.println("---------------");
        Optional<String> s =  Stream.of(new String[]{"string", "is", "too", "short"}).findFirst();
        System.out.println("s = " + s.get());
        System.out.println("---------------");

        //test Optional
        String text = "hah";
        Optional.ofNullable(text).ifPresent(System.out::println);
        System.out.println("---------------");
        System.out.println(Optional.ofNullable(text).map(String::length).orElse(-1));
        System.out.println("---------------");

        Stream.of(new String[]{"this","is", "test"}).min((o1,o2)->o1.length()>o2.length()?1:-1).ifPresent(System.out::println);


    }
}
